<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<!-- <el-form-item>
					<el-input v-model="filters.name" placeholder="设备IP"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary">查询</el-button>
				</el-form-item> -->
				<el-form-item v-show="my_permi.indexOf('settlementWeight.store') > -1 || my_permi.indexOf('admin') > -1">
					<el-button type="primary" @click="tableVisible = !tableVisible">新增</el-button>
				</el-form-item>
				<el-date-picker
			      v-model="exprotDate"
			      type="daterange"
      			  range-separator="至"
			      placeholder="请选择开始和结束时间">
			    </el-date-picker>
				<el-form-item v-show="my_permi.indexOf('settlementWeight.export') > -1 || my_permi.indexOf('admin') > -1">
					<el-button type="primary" @click="exportT">导出</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="tableList" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="clientHeight" >
			<el-table-column prop="vehicle_number" label="车牌号" width="100">
			</el-table-column>
			<el-table-column prop="driver_name" label="司机姓名" width="120">
			</el-table-column>
			<el-table-column prop="driver_idcard" label="身份证号码" width="140">
			</el-table-column>
			<el-table-column prop="driver_mobile_phone" label="手机号码" width="80">
			</el-table-column>
			<el-table-column prop="actual_weight" label="实际(吊秤)重量(KG)" width="130">
			</el-table-column>
			<el-table-column prop="system_datetime" label="装货日期" width="80">
			</el-table-column>
			<el-table-column prop="is_weight_desc" label="是否过磅" width="70">
			</el-table-column>
			<el-table-column prop="operator" label="操作人员" width="70">
			</el-table-column>
			<el-table-column prop="updae_datetime" label="更新时间" width="80">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button v-show="my_permi.indexOf('settlementWeight.show') > -1 || my_permi.indexOf('admin') > -1" size="small" type="primary" @click="router({path: '/logistics/weight/settlementWeightDetail', query: {systemNumber: scope.row.system_number}})">详情</el-button>
					<el-button v-show="my_permi.indexOf('settlementWeight.put') > -1 || my_permi.indexOf('admin') > -1" type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button v-show="my_permi.indexOf('settlementWeight.delete') > -1 || my_permi.indexOf('admin') > -1" type="danger" size="small" @click="del(scope.row.system_number)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination
				layout="prev, pager, next"
				@current-change="handleCurrentChange"
				:total="total"
				style="float:right;">
			</el-pagination>
		</el-col>

		<!-- 新增选择称重记录表 -->
		<el-dialog title="新增" v-model="tableVisible" :close-on-click-modal="false">
			<el-table :data="tableList2" highlight-current-row v-loading="listLoading2" style="width: 100%;cursor: pointer;" @row-click="changeTable">
				<el-table-column prop="ic_id" label="IC卡卡号">
				</el-table-column>
				<el-table-column prop="trans_status_desc" label="单据类型">
				</el-table-column>
				<el-table-column prop="trans_type_desc" label="单据状态">
				</el-table-column>
				<el-table-column prop="vehicle_number" label="车牌号码">
				</el-table-column>
				<el-table-column prop="is_finished_desc" label="是否完成" width="110">
				</el-table-column>
				<el-table-column prop="site_code" label="称重计量地点">
				</el-table-column>
			</el-table>
		</el-dialog>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="车牌号">
					<el-input v-model="addForm.vehicle_number" readonly auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="司机姓名">
					<el-input v-model="addForm.driver_name" readonly auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="身份证号码">
					<el-input v-model="addForm.driver_idcard" readonly auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号码">
					<el-input v-model="addForm.driver_mobile_phone" readonly auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="实际(吊秤)重量(KG)">
					<el-input v-model="addForm.actual_weight" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="装货日期">
					<el-date-picker type="date" placeholder="选择日期" v-model="addForm.system_datetime"></el-date-picker>
				</el-form-item>
				<el-form-item label="是否过磅">
					<el-select v-model="addForm.is_weight" placeholder="请选择">
					    <el-option
					      v-for="item in options"
					      :key="item.value"
					      :label="item.label"
					      :value="item.value">
					    </el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--编辑界面-->
		<el-dialog title="新增" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<!-- <el-form-item label="车牌号">
					<el-input v-model="editForm.vehicle_number" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="司机姓名">
					<el-input v-model="editForm.driver_name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="身份证号码">
					<el-input v-model="editForm.driver_idcard" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号码">
					<el-input v-model="editForm.driver_mobile_phone" auto-complete="off"></el-input>
				</el-form-item> -->
				<el-form-item label="实际(吊秤)重量(KG)">
					<el-input v-model="editForm.actual_weight" auto-complete="off"></el-input>
				</el-form-item>
				<!-- <el-form-item label="装货日期">
					<el-date-picker type="date" placeholder="选择日期" v-model="editForm.system_datetime"></el-date-picker>
				</el-form-item>
				<el-form-item label="是否过磅">
					<el-select v-model="editForm.is_weight" placeholder="请选择">
					    <el-option
					      v-for="item in options"
					      :key="item.value"
					      :label="item.label"
					      :value="item.value">
					    </el-option>
					</el-select>
				</el-form-item> -->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '@/common/js/util'
	import { settlementWeight, settlementWeightDetail, getIsWeight, weightData, getCards, BASEURL } from '@/api/api';
    import { mapState } from 'vuex'
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				tableList: [],
				total: 0,
				page: 1,
				listLoading: false,
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				options: [],
				//新增界面数据
				addForm: {
					vehicle_number: '',
					driver_name: '',
					driver_idcard: '',
					driver_mobile_phone: '',
					actual_weight: '',
					system_datetime: '',
					is_weight: '',
				},
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				//编辑界面数据
				editForm: {
					vehicle_number: '',
					driver_name: '',
					driver_idcard: '',
					driver_mobile_phone: '',
					actual_weight: '',
					system_datetime: '',
					is_weight: '',
				},
				listLoading2: false,
				tableList2: [],
				total2: 0,
				page2: 1,
				tableVisible: false,
				icid: '',
				exprotDate: '',
			}
		},
		methods: {
			router(path) {
				this.$router.push(path)
			},
			handleCurrentChange(val) {
				this.page = val;
				this.settlement_weight();
			},
			//获取设备警报信息
			settlement_weight() {
				this.listLoading = true;
				settlementWeight(this.page).then((res) => {
					if(res.data.status.errCode == '10002'){
						sessionStorage.removeItem('token');
						this.$message({
							message: res.data.status.message,
							type: 'warning'
				        });
				        setTimeout(() =>{
				        	this.router({path: '/login'})
				        }, 1500);
						return;
					}
					this.total = res.data.data.total
					this.tableList = res.data.data.data
					this.listLoading = false
				});
			},
			del(system_number) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					settlementWeightDetail(system_number, 'DELETE').then((res) => {
						this.listLoading = false;
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.settlement_weight();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.system_number = row.system_number;
				this.editForm = {
					vehicle_number: row.vehicle_number,
					driver_name: row.driver_name,
					driver_idcard: row.driver_idcard,
					driver_mobile_phone: row.driver_mobile_phone,
					actual_weight: row.actual_weight,
					system_datetime: row.system_datetime,
					is_weight: row.is_weight,
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							para.system_datetime = (!para.system_datetime || para.system_datetime == '') ? '' : util.formatDate.format(new Date(para.system_datetime), 'yyyy-MM-dd');
							settlementWeightDetail(this.system_number, 'PUT', para).then((res) => {
								if(res.data.status.errCode != '0'){
									this.$message({
										message: res.data.status.message,
										type: 'error'
									});
								}else{
									this.$message({
										message: res.data.status.message,
										type: 'success'
									});
								}
								this.editLoading = false;
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.settlement_weight();
							}).catch((err) => {
								console.dir(err)
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							para.system_datetime = (!para.system_datetime || para.system_datetime == '') ? '' : util.formatDate.format(new Date(para.system_datetime), 'yyyy-MM-dd');
							settlementWeightDetail('', 'POST', para).then((res) => {
								if(res.data.status.errCode != '0'){
									this.$message({
										message: res.data.status.message,
										type: 'error'
									});
								}else{
									this.$message({
										message: res.data.status.message,
										type: 'success'
									});
								}
								this.addLoading = false;
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.settlement_weight();
							});
						});
					}
				});
			},
			// 结算重量是否过磅
			card_status() {
				// getIsWeight().then((res) => {
					this.options = [];
					let arrs = [
						{
							key: 'N',
							value: '未过磅'
						},
						{
							key: 'Y',
							value: '已过磅'
						}
					]
					for(var i in arrs){
						var arr = {
							label: arrs[i].value,
							value: arrs[i].key,
						}
						this.options.push(arr)
					}
				// })
			},
			handleCurrentChange(val) {
				this.page2 = val;
				this.weight_data();
			},
			//获取设备警报信息
			weight_data() {
				this.listLoading2 = true;
				weightData(this.page2, true).then((res) => {
					if(res.data.status.errCode == '10002'){
						sessionStorage.removeItem('token');
						this.$message({
							message: res.data.status.message,
							type: 'warning'
				        });
				        setTimeout(() =>{
				        	this.router({path: '/login'})
				        }, 1500);
						return;
					}
					this.listLoading2 = false
					this.total2 = res.data.data.total
					this.tableList2 = res.data.data.data
				});
			},
			changeTable(row) {
				this.tableVisible = false
				this.addFormVisible = true
				this.icid = row.ic_id
				getCards(this.icid).then((res) => {
						console.log(res.data.status)
					if(res.data.status.errCode == '0'){
						console.log(res.data.data.vehicle_number)
						this.addForm = {
							vehicle_number: res.data.data.vehicle_number,
							driver_name: res.data.data.driver_name,
							driver_idcard: res.data.data.driver_idcard,
							driver_mobile_phone: res.data.data.driver_mobile_phone,
							actual_weight: '',
							system_datetime: '',
							is_weight: '',
						}
					}
				});
			},
			exportT() {
				let startD = "";
				let overD = "";
				let firstDate = "";
				let secendDate = "";
				if(this.exprotDate){
					firstDate = new Date(this.exprotDate[0])
					secendDate = new Date(this.exprotDate[1])
				}else{
					firstDate = new Date();
					secendDate = new Date();
				}
				startD = firstDate.getFullYear() + '-' + (firstDate.getMonth()+1) + '-' + firstDate.getDate()
				overD = secendDate.getFullYear() + '-' + (secendDate.getMonth()+1) + '-' + secendDate.getDate()
				location.href=`${BASEURL}/SettlementWeight/Export?token=${sessionStorage.token}&start_time=${startD}&end_time=${overD + ' 23:59:59'}&pageSize=${sessionStorage.getItem('pagesize')}`;
			},
		},
        computed: {

            ...mapState(
                {
                  'my_permi': state => state.my_permi,
                  'clientHeight': state => state.clientHeight
                }
            )
        },
		mounted() {
			this.settlement_weight();
			this.card_status();
			this.weight_data();
		}
	}

</script>

<style scoped>

</style>